/**
 * Project: greenline-epay-biz-core
 * 
 * File Created at 2013-3-29
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.pay.biz.manager.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang.StringUtils;

/**
 * @Type RmsDateUtils
 * @Desc
 * @author jianyun.zheng
 * @date 2013-3-29
 * @Version V1.0
 */
public class DateUtils {

    private static final String DEFAULT_F = "yyyyMMdd";

    /**
     * 在当前年份基础上，增加年份
     * 
     * @param amount
     * @return Date
     */
    public static Date addYear(int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, amount);
        return calendar.getTime();
    }

    public static Date addDay(int diff) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, diff);
        return calendar.getTime();
    }

    /**
     * 在给定的时间上加指定的天数
     * 
     * @param startDate
     * @param diff
     * @return
     */
    public static Date addDay(Date startDate, int diff) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(Calendar.DAY_OF_MONTH, diff);
        return calendar.getTime();
    }

    public static Date addMonth(int diff) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, diff);
        return calendar.getTime();
    }

    public static Date addHour(int diff) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, diff);
        return calendar.getTime();
    }

    public static Date getDateByString(String date, String format) {
        if (StringUtils.isNotBlank(date)) {
            try {
                if (StringUtils.isBlank(format)) {
                    SimpleDateFormat defm = new SimpleDateFormat(DEFAULT_F);
                    return defm.parse(date);
                }
                SimpleDateFormat fm = new SimpleDateFormat(format);
                return fm.parse(date);
            } catch (ParseException e) {
            }
        }
        return null;
    }

    /**
     * 获取日期，格式为yyyy-MM-dd HH:mm:ss
     * 
     * @param d
     * @return String
     */
    public static String dateFormat(Date d, String format) {
        try {
            if (null == d) {
                return null;
            }
            if (StringUtils.isBlank(format)) {
                SimpleDateFormat defm = new SimpleDateFormat(DEFAULT_F);
                return defm.format(d);
            }
            SimpleDateFormat fm = new SimpleDateFormat(format);
            return fm.format(d);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 获取日期，格式为yyyy-MM-dd HH:mm:ss
     * 
     * @param d
     * @return String
     */
    public static String dateFormat(String d, String oriFormat, String newformat) {
        try {
            if (null == d || null == oriFormat || null == newformat) {
                return null;
            }
            SimpleDateFormat fm = new SimpleDateFormat(newformat);
            SimpleDateFormat ori = new SimpleDateFormat(oriFormat);
            return fm.format(ori.parse(d));
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 设置当前日期的最大时间
     * 
     * @return
     */
    public static Date getTodayEndTime(Date date) {
        Calendar currentDate = new GregorianCalendar();
        if (null != date) {
            currentDate.setTime(date);
        }
        currentDate.set(Calendar.HOUR_OF_DAY, 23);
        currentDate.set(Calendar.MINUTE, 59);
        currentDate.set(Calendar.SECOND, 59);
        currentDate.set(Calendar.MILLISECOND, 999);
        return currentDate.getTime();
    }

    /**
     * 设置当前日期的最小时间
     * 
     * @return
     */
    public static Date getTodayStartTime(Date date) {
        Calendar currentDate = new GregorianCalendar();
        if (null != date) {
            currentDate.setTime(date);
        }
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        currentDate.set(Calendar.MILLISECOND, 0);
        return currentDate.getTime();
    }

    /**
     * 根据String得到清算日期date格式
     * 
     * @param dateString
     * @return
     * @throws ParseException
     */
    public static Date toEpaySettleDate(String dateString) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_F);
        return simpleDateFormat.parse(dateString);
    }

    /**
     * 根据date得到清算日期String格式
     * 
     * @param date
     * @return
     */
    public static String toEpaySettleString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_F);
        return simpleDateFormat.format(date);
    }

    /**
     * 获取日期，格式为yyyy-MM-dd HH:mm:ss
     * 
     * @param d
     * @return String
     */
    public static Date dateParse(String d, String format) {
        try {
            if (null == d) {
                return null;
            }
            if (StringUtils.isBlank(format)) {
                SimpleDateFormat defm = new SimpleDateFormat(DEFAULT_F);
                return defm.parse(d);
            }
            SimpleDateFormat fm = new SimpleDateFormat(format);
            return fm.parse(d);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 比较两个日期
     * 
     * @param start
     * @param end
     * @param format
     * @return
     */
    public static boolean compare(String start, String end, String format1, String format2) {
        if (!StringUtils.isEmpty(start) || !StringUtils.isEmpty(end)) {
            Date date1 = dateParse(start, format1);
            Date date2 = dateParse(end, format2);
            if (date1.getTime() < date2.getTime()) {
                return true;
            }
        }
        return false;
    }

}
